package com.stedu.mall.adminservice.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.stedu.mall.adminservice.mapper.AdminMapper;
import com.stedu.mall.common.bean.Admin;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminSeviceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(String username, String password) throws SteduException {
        //根据用户名查询
        Admin admin = adminMapper.selectByUsername(username);
        if (admin == null) {
            throw new SteduException("用户名错误");
        }
        //判断是否禁用
        if (admin.getStatus() == 0) {
            throw new SteduException("该用户已禁用，请联系管理员");
        }
        //对用户密码进行加密-使用MD5算法和盐进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(password + admin.getSalt()));
        //对加密之后的密码和数据库密码进行比较
        if (!md5Pwd.equals(admin.getPassword())) {
            throw new SteduException("密码错误");
        }

        return admin;
    }
    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectById(id);

    }
    @Override
    public boolean update(Admin admin) {
        return adminMapper.update(admin) == 1;
    }
    @Override
    public boolean chgPwd(String oldPwd, String newPwd,Integer id) throws SteduException {
        //判断原密码是否正确
        //根据用户id查询用户信息，获取原密码和盐
        Admin admin = adminMapper.selectById(id);
        String salt = admin.getSalt();
        //使用用户输入原密码和盐加密 --> 加密之后的用户输入的密码
        String md5OldPwd = SecureUtil.md5(SecureUtil.md5(oldPwd + salt));
        //和数据库中的原密码进行比较
        if(!admin.getPassword().equals(md5OldPwd)){
            throw new SteduException("原密码错误");
        }
        //对新密码结合盐进行加密
        String md5NewPwd = SecureUtil.md5(SecureUtil.md5(newPwd + salt));
        //判断两次密码是否相同
        if (md5NewPwd.equals(admin.getPassword())) {
            throw new SteduException("新密码与原密码相同，修改失败");
        }
        //修改密码
        admin = new Admin();
        admin.setId(id);
        admin.setPassword(md5NewPwd);
        return adminMapper.update(admin) == 1;
    }
    @Override
    public boolean insert(Admin admin) throws SteduException {
        if (adminMapper.selectByUsername(admin.getUsername()) != null) {
            throw new SteduException("该用户已存在，添加失败");
        }
        String salt = RandomUtil.randomString(8);
        admin.setSalt(salt);
        admin.setPassword(SecureUtil.md5(SecureUtil.md5(admin.getPassword() + salt)));
        return adminMapper.insert(admin) == 1;
    }
    @Override
    public List<Admin> selectAll() {
        return adminMapper.selectAll();
    }
    @Override
    public boolean delete(Integer id) throws SteduException {
        if (adminMapper.selectById(id) == null) {
            throw new SteduException("该用户不存在，删除失败");
        }
        return adminMapper.delete(id) == 1;
    }
    @Override
    public boolean resetPassword(Integer id, String newPassword) {
        //判断原密码是否正确
        Admin admin = adminMapper.selectById(id);
        String salt = admin.getSalt();
        //对新密码结合盐进行加密
        String newMd5 = SecureUtil.md5(SecureUtil.md5(newPassword + salt));
        admin = new Admin();
        admin.setId(id);
        admin.setPassword(newMd5);
        //修改
        return adminMapper.update(admin) == 1;
    }

    @Override
    public boolean updateInfo(Admin admin) {
        return adminMapper.update(admin) == 1;
    }


}
